-module(fp).
-compile(export_all).
% -export([start/0]).

% start() ->
%     io:format("hello world ~n").

add1(X) -> X+1.

test() ->
    Plus1 = fun add1/1,
    Plus1(7).

test2(F) ->
    F(7).

print_two_param(X,Y) ->
    io:format("X is ~p, Y is ~p~n", [X,Y]).

cu_print_two_param(X) ->
    fun(Y) ->
        io:format("X is ~p, Y is ~p~n", [X,Y]),
        void
    end.

% curry函数，入参为一个两参数函数，返回为一参数函数
curry(Fun) ->
    fun(X) ->
        fun(Y) ->
            Fun(X,Y)
        end
    end.

test7() ->
    F2param = fun print_two_param/2,
    % F2param("a", "b").
    F1parma = curry(F2param),
    (F1parma("a"))("b").

curry3(Fun) ->
    fun(X) ->
        fun(Y) ->
            fun(Z) ->
                Fun(X,Y,Z)
            end
        end
    end.

% 依赖注入
adder_with_plugin_logger(Logger,X,Y) ->
    Logger("X is ~p~n",[X]),
    Logger("Y is ~p~n",[Y]),
    Result = X+Y,
    Logger("X+Y is ~p~n", [Result]),
    Result.

console_logger(Format,Args) ->
    io:format(Format,Args).

test8() ->
    Adder = curry3(fun adder_with_plugin_logger/3),
    AdderConsole = Adder(fun console_logger/2),
    (AdderConsole(2))(3).